/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 地市下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectAreaTag />
 * 			   (最常用模式)：<htdz-tag:SelectAreaTag selected="${xxx}"/>
 * 参数说明：
 * 1. name可以不写,缺省值为"areaid"
 * 2. id可以不写,缺省值为name的值
 * 3. onchange可以不写，缺省值为无
 * 4. firstoption为首个下拉框的text。可以不写，缺省值为"全部"；
 *    如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则表示此下拉框不需要添加额外option
 * 5. firstvalue为首个下拉框的value。可以不写，缺省值为"MyConstant.SELECT_FIRST_VALUE"
 * 6. selected为下拉框选中的值，可以不写。
 * 7. hasprov：是否需要添加“全省”，true：添加，firstoption默认修改为"请选择"；false：不添加；缺省值为false。
 * 8. disabled：是否禁用true/false，缺省值为false
 * 9. orgacode：父组织编码，可以不写。如果有值，则表示显示子组织所属地区下拉框
 * 10. hidedesc：是否隐藏文字说明“地市：”，缺省值为显示
 * 11.cityonly:是否只显示一个地市，缺省值为false。当用户为地市组织时，有时只需要显示该用户所在地市即可，当为true时,selected值为必填项；
 * 
 * 修订列表： v1.00 2009-12-15 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 *          v1.01 2010-03-15 
 * 			修订人：董一华 
 * 			修订内容：新增：hasprov，disabled
 * 
 * 			v1.02 2010-04-18 
 * 			修订人：董一华 
 * 			修订内容：新增：orgacode
 * 
 * 			v1.03 2010-06-05 
 * 			修订人：董一华 
 * 			修订内容：新增：hidedesc,删除hasprov
 * 
 *          v1.04 2011-08-26 
 * 			修订人：董一华 
 * 			修订内容：新增：cityonly
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class SelectAreaTag extends TagSupport {
	private static final long serialVersionUID = 7343412033252888637L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private boolean hasprov;
	private boolean disabled;
	private String orgacode;
	private boolean hidedesc;// 是否隐藏文字说明“地市：”
	private boolean cityonly;

	public SelectAreaTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {

		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		boolean hasprov = this.hasprov;
		boolean disabled = this.disabled;
		String orgacode = this.orgacode;
		boolean hidedesc = this.hidedesc;
		boolean cityonly = this.cityonly;

		if (name == null || name.length() == 0) {
			name = "areaid";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		StringBuilder html = new StringBuilder();
		try {
			if (!hidedesc) {
				html.append("地市：");
			}
			html.append("<select name=\"");
			html.append(name);
			html.append("\"	id=\"");
			html.append(id);
			html.append("\" ");
			if (onchange != null && onchange.length() > 0) {
				html.append("onchange=\"");
				html.append(onchange);
				html.append("\"");
			}
			if (disabled)// 禁用
				html.append(" disabled ");
			html.append(">");

			if (cityonly && !MyFormat.isStrNull(selected)) {// 只显示一个地市
				html.append("<option value=\"");
				html.append(selected);
				html.append("\">");
				html.append(MyParamUtil.getAreaName(selected));
				html.append("</option>");
			} else {
				if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
					if (firstoption == null || firstoption.length() == 0)
						firstoption = hasprov ? "请选择" : "全部";

					if (firstvalue == null || firstvalue.length() == 0)
						firstvalue = MyConstant.SELECT_FIRST_VALUE;

					html.append("<option value=\"");
					html.append(firstvalue);
					html.append("\">");
					html.append(firstoption);
					html.append("</option>");
				}
				if (hasprov)// 添加“全省”
				{
					html.append("<option value=\"0\" ");
					if ("0".equals(selected))
						html.append("selected");
					html.append(">全省</option>");
				}

				if (orgacode != null && orgacode.length() > 0) {// 显示子组织下拉框
					if (!MyConstant.SELECT_FIRST_VALUE.equals(orgacode)) {
						MyJDBC myJDBC = null;
						try {
							myJDBC = new MyJDBC();
							String strSQL = "SELECT a.areaid,a.areaname FROM ORGANIZA o,AREA a WHERE o.parentorgacode=? AND a.areaid=o.areaid AND o.status='1' ORDER BY a.areaid";
							PreparedStatement ps = myJDBC.prepareStatement(strSQL);
							ps.setString(1, orgacode);
							ResultSet rs = ps.executeQuery();
							String areaid = "";
							String areaname = "";
							while (rs.next()) {
								areaid = rs.getString("areaid");
								areaname = rs.getString("areaname");
								html.append("<option value=\"");
								html.append(areaid);
								html.append("\" ");
								if (areaid.equals(selected))
									html.append("selected");
								html.append(">");
								html.append(areaname);
								html.append("</option>");
							}
							rs.close();
							rs = null;
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							MyJDBC.close(myJDBC);
						}
					}
				} else if (MyParam.hAreaIDToAreaName != null && MyParam.hAreaIDToAreaName.size() > 0) {// 全部地市
					Iterator<Map.Entry<String, String>> iterator = MyParam.hAreaIDToAreaName.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append("<option value=\"");
						html.append(key);
						html.append("\" ");
						if (selected != null && selected.length() > 0 && key.equals(selected))
							html.append("selected");
						html.append(">");
						html.append(value);
						html.append("</option>");
					}
				}
			}
			html.append("</select>");
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public void setHasprov(boolean hasprov) {
		this.hasprov = hasprov;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = (disabled != null && "true".equals(disabled.trim().toLowerCase()));
	}

	public void setOrgacode(String orgacode) {
		this.orgacode = orgacode;
	}

	public void setHidedesc(boolean hidedesc) {
		this.hidedesc = hidedesc;
	}

	public void setCityonly(boolean cityonly) {
		this.cityonly = cityonly;
	}

	public void setCityonly(String cityonly) {
		this.cityonly = (cityonly != null && "true".equals(cityonly.trim().toLowerCase()));
	}
}
